﻿namespace ufida.u9.ait.openbp
{
	using System;
	using System.Collections.Generic;
	using System.Text;
    using UFIDA.U9.AIT.OpenAPIBE;
    using UFIDA.U9.MO.MO;
    using UFSoft.UBF.AopFrame;
    using UFSoft.UBF.Business;
    using UFSoft.UBF.Util.Context;

	/// <summary>
	/// RecalcMOPickListsByBOMTaskBP partial 
	/// </summary>	
	public partial class RecalcMOPickListsByBOMTaskBP 
	{	
		internal BaseStrategy Select()
		{
			return new RecalcMOPickListsByBOMTaskBPImpementStrategy();	
		}		
	}

    #region  implement strategy	
    /// <summary>
    /// 调用MO标准功能,进行备料重算
    /// 该BP通过脚本注册为定时任务,任务名:生产订单备料重算定时任务
    /// 使用时,在 请求管理 中添加,应用=基础设置,异步程序=MO备料重算定时任务
    /// </summary>	
    internal partial class RecalcMOPickListsByBOMTaskBPImpementStrategy : BaseStrategy
	{
		public RecalcMOPickListsByBOMTaskBPImpementStrategy() { }

		public override object Do(object obj)
		{						
			RecalcMOPickListsByBOMTaskBP bpObj = (RecalcMOPickListsByBOMTaskBP)obj;

            //查找MO集合,状态=开工 累计完工=0
            MO.EntityList molist = MO.Finder.FindAll("DocState=2 and TotalCompleteQty=0", null);
			List<MO.EntityKey> molist1=new List<MO.EntityKey>();

			using(ISession session = Session.Open())
			{
				//查找MO并记录日志
                foreach (MO mo in molist)
                {
                    if (mo.DocState == UFIDA.U9.MO.Enums.MOStateEnum.Released)
                    {
                        molist1.Add(mo.Key);

						//记录重算日志
                        AITLog log = AITLog.Create();
                        log.Code = mo.DocNo;
                        log.Name = "MO备料重算";
                    }
                }

				session.Commit();
            }
			
			//调用系统标准BP MO备料重算
            RecalcMOPickListsByBOM bp = new RecalcMOPickListsByBOM();
            bp.MOKeys = molist1;
            bp.Do();

            return null;
		}		
	}

	#endregion
	
	
}